/*
  Copyright (c) 2005, Ulrich Hilger, Light Development, http://www.lightdev.com
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification, 
  are permitted provided that the following conditions are met:

    - Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
       
    - Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
       
    - Neither the name of Light Development nor the names of its contributors may be 
       used to endorse or promote products derived from this software without specific 
       prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*/

package primerdb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

import java.util.ArrayList;
import java.util.Vector;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import java.util.Properties;




import java.util.Vector;
import java.util.Properties;
import primerdb.persistentTree.HierarchicalDataStore;
import primerdb.persistentTree.HierarchicalItem;
import primerdb.persistentTree.Region;
import primerdb.persistentTree.StorageException;

/**
 * RegionStoreAdapter.java
 * 
 * <p>In the generic context of JPersistentTree and TreeStorageController this class 
 * is the specific implementation required to actually store to and retrieve 
 * from a particular data store. Typically for each data store such a class needs 
 * to be built. However, depending on the actual implementation such class can as well be 
 * re-used for various data stores too. E.g. when implementing a variable way to connect to a 
 * JDBC data source such RegionStore class could be re-used with different region data 
 * sources accordingly.</p> 
 * 
 * <p>To not make our example not overly complicated, RegionStoreAdapter implements interface 
 * HierarchicalDataStore specifically for reading and writing via JDBC to and from 
 * a relational MySQL database using fixed schema, table and field names. It refers to 
 * a database table as defined with the SQL script 'create_region_table.sql' included 
 * in this example package.</p>
 * 
 * <p>What should be demonstrated is the fact that the persistent tree framework can remain 
 * unchanged entirely for all kinds of data stores. Only the part relating to the specific 
 * data store, i.e. type (file, database, XML, etc.) and the way to connect 
 * (local, JDBC, app server, etc.) needs to be implementend individually with a single adapter 
 * class such as RegionStoreAdapter.</p>
 * 
 * <p>Certainly the framework can be extended by adding generic JDBCDataStores, XMLDataStores, 
 * RPCDataStores, etc. but this would exceed the scope of this example.</p>
 * 
 * @author Ulrich Hilger
 * @author Light Development
 * @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
 * @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
 * @author published under the terms and conditions of the BSD License,
 *      for details see file license.txt in the distribution package of this software
 *
 * @version 1, 18.09.2005
 */
public class FolderStoreAdapter implements HierarchicalDataStore {

  private PrimerDao db;

  public FolderStoreAdapter(PrimerDao db) {
    super();
    this.db = db;
  }

  /**
   * get the item with the given id
   * @param id  the id of the item to get
   * @return the item with the given id or null, if none was found with that id
   * @throws StorageException
   */
  public HierarchicalItem getItem(Object id) throws StorageException {
    Folder f = null;
    f = db.getFolder( (Integer) id );
    
    return f;
    
    /*
    String sql = "select * from folder where id=" + id.toString();
    try {
      ResultSet rs = getConnection().createStatement().executeQuery(sql);
      if (rs.next()) {
        r = new Region(rs.getInt("rgId"), rs.getString("rgName"));
        r.setParentId(rs.getInt("parentId"));
      }
      rs.close();
    }
    catch (SQLException e) {
      throw new StorageException(e);
    }
    return r; */
  }

  /**
   * get all child items of a given item
   * @param id the id of the item to get child items for
   * @return  a Vector of HierarchicalItem objects 
   * or null if an item with the given id was not found
   * @throws StorageException
   */
  public Vector<HierarchicalItem> getChildItems(Object id) throws StorageException {
    Vector<HierarchicalItem> v = new Vector();
    v = db.getListFolders(Integer.parseInt(id.toString()));
    /*
    StringBuffer sql = new StringBuffer();
    sql.append("select * from regions where parentId=");
    sql.append(id.toString());
    try {
      ResultSet rs = getConnection().createStatement().executeQuery(sql.toString());
      while(rs.next()) {
          Region r = new Region(rs.getInt("rgId"), rs.getString("rgName"));
          r.setParentId(id);
          v.addElement(r);
      }
    }
    catch (SQLException e) {
      throw new StorageException(e);
    }*/
    return v;
  }

  /**
   * get all root items in the data store, i.e. all top level items in the item 
   * hierarchy that have no parent (parent ID = -1)
   * @return a Vector of HierarchicalItems with all root items
   * @throws StorageException
   */
  
  public Vector<HierarchicalItem> getRootItems() throws StorageException {
    System.out.println("Getting root items");
    Vector<HierarchicalItem> v = new Vector();
    
    v = db.getListFolders(0);
    
    /*
    StringBuffer sql = new StringBuffer();
    sql.append("select * from ");
    //sql.append(dbName);
    sql.append("regions where parentId=0");
    try {
      ResultSet rs = getConnection().createStatement().executeQuery(sql.toString());
      //if (rs.first() && !rs.isAfterLast()) {
        while(rs.next()) {
          Region r = new Region(rs.getInt("rgId"), rs.getString("rgName"));
          r.setParentId(new Integer(-1));
          v.addElement(r);          
        }
      //}
      //else {
      //  v = null;
      //}
    }
    catch (SQLException e) {
      throw new StorageException(e);
    }
     * 
     */
    return v;

   }
	
  /**
   * store an item with the given id
   * @param item  the item to store
   * @return the item that has been stored or null if no item with that id was found
   * @throws StorageException
   */
  public HierarchicalItem putItem(HierarchicalItem item) throws StorageException {
    HierarchicalItem returnItem = null;
    
    Boolean b = db.editFolderName(item); 
    
    /*
    StringBuffer sql = new StringBuffer();
    sql.append("update regions set rgName='");
    sql.append(item.getData().toString());
    sql.append("', parentId=");
    sql.append(item.getParentId().toString());
    sql.append(" where rgId=");
    sql.append(item.getId().toString());
    try {
      int result = getConnection().createStatement().executeUpdate(sql.toString());
      if(result > 0) {
        returnItem = item;
      }
    }
    catch (SQLException e) {
      throw new StorageException(e);
    }
    */
    if(b) returnItem = item;
    
    return returnItem;
  }

  /**
   * create a new item in the data store
   * @param item  the new item to create
   * @return  the id of the newly create item
   * @throws StorageException
   */
  public Object createItem(HierarchicalItem item) throws StorageException {
    System.out.println("Creating item with parentid:" + item.getParentId().toString());
    Integer newId;
    newId = db.newFolder(item);
    /*
    Integer newId = null;
    StringBuffer sql = new StringBuffer();
    sql.append("insert into regions (rgName, parentId) values ('");
    sql.append(item.getData().toString());
    sql.append("', ");
    sql.append(item.getParentId().toString());
    sql.append(") ");
    try {
      Connection c = getConnection();
      c.createStatement().executeUpdate(sql.toString());
      sql = new StringBuffer();
      sql.append("select rgId from regions where rgName='");
      sql.append(item.getData().toString());
      sql.append("'");
      ResultSet rs = c.createStatement().executeQuery(sql.toString());
      if (rs.next()) {
        newId = rs.getInt("rgId");
      }
      rs.close();
    }
    catch(Exception e) {
      throw new StorageException(e);
    }
    */
    return newId;
  }

  /**
   * delete the item with the given id from the data store 
   * @param id
   * @return  the id of the deleted item or null if an item with the given id was not found
   * @throws StorageException
   */
  public Object deleteItem(Object id) throws StorageException {
    Object deletedId = null;
    
    Boolean b = db.deleteFolder(Integer.parseInt(id.toString()));
    
    /*
    StringBuffer sql = new StringBuffer();
    sql.append("delete from ");
    //sql.append(dbName);
    sql.append("regions where rgId=");
    sql.append(id.toString());
    try {
      int result = getConnection().createStatement().executeUpdate(sql.toString());
      if(result > 0) {
        deletedId = id;
      }
    }
    catch (SQLException e) {
      throw new StorageException(e);
    } */
    if(b) deletedId = id;
    
    return deletedId;
  }
  
  public Object updateItem(HierarchicalItem item, Object id) throws StorageException {
    Object updatedItem = null;
    
    Boolean b = db.editFolderParent(item,id);
    
    if(b) updatedItem = item;
    
    return updatedItem;
  }

  
  
  /**
   * get a database connection, create and cache one, if not done 
   * already.
   * 
   * a mysql connect string looks as follows
   * jdbc:mysql://server/db?user=name&password=pw
   */
//  private Connection getConnection() {
//    
//    if(connection == null) {
//      try {         
//        setDBSystemDir();
//        dbProperties = loadDBProperties();        
//        String dbUrl = getDatabaseUrl();
//        String driverName = dbProperties.getProperty("derby.driver"); 
//        loadDatabaseDriver(driverName);
//        if(!dbExists()) {
//            Boolean b = createDatabase();
//            if(b) {
//               System.out.println("Database created successfully");
//            }
//        }
//        connection = DriverManager.getConnection(dbUrl, dbProperties);
//        
//        System.out.println(dbProperties.toString());
//        
//      }
//      catch (Exception e) {
//        e.printStackTrace();
//      }      
//    }
//    return connection;
//  }
//
//  /**
//   * close the connection this adapter holds (if any)
//   */
//  public void closeConnection() {
//    if(connection != null) {
//      try {
//        connection.close();
//        connection = null;
//      }
//      catch (SQLException e) {
//        e.printStackTrace();
//      }
//    }
//  } 

//  private Connection connection;
//  private String serverName;
//  private String userName;
//  private String password;
//  private String dbName;
  
  
  
  
  
    
//    private boolean dbExists() {
//        boolean bExists = false;
//        String dbLocation = getDatabaseLocation();
//        File dbFileDir = new File(dbLocation);
//        if (dbFileDir.exists()) {
//            bExists = true;
//        }
//        return bExists;
//    }
//    
//    private void setDBSystemDir() {
//        // decide on the db system directory
//        String userHomeDir = System.getProperty("user.home", ".");
//        String systemDir = ".primerdb";
//        System.setProperty("derby.system.home", systemDir);
//        
//        // create the db system directory
//        File fileSystemDir = new File(systemDir);
//        fileSystemDir.mkdir();
//    }
//    
//    private void loadDatabaseDriver(String driverName) {
//        // load Derby driver
//        try {
//            Class.forName(driverName);
//        } catch (ClassNotFoundException ex) {
//            ex.printStackTrace();
//        }
//    }
//    
//    private Properties loadDBProperties() {
//        InputStream dbPropInputStream = null;
//        dbPropInputStream = RegionStoreAdapter.class.getResourceAsStream("Configuration.properties");
//        dbProperties = new Properties();
//        try {
//            dbProperties.load(dbPropInputStream);
//        } catch (IOException ex) {
//            ex.printStackTrace();
//        }
//        
//        System.out.print("derby url = " + dbProperties.getProperty("derby.url"));
//        
//        return dbProperties;
//    }
//    
//    
//    private boolean createTables(Connection connection) {
//        boolean bCreatedTables = false;
//        Statement statement = null;
//        try {
//            statement = connection.createStatement();
//            statement.execute(strCreateRegionTable);
//            statement.execute(strInsertRoot);
//            bCreatedTables = true;
//        } catch (SQLException ex) {
//            ex.printStackTrace();
//        }
//        
//        return bCreatedTables;
//    }
//    
//    
//    private boolean createDatabase() {
//        boolean bCreated = false;
//        Connection connection = null;
//        
//        String dbUrl = getDatabaseUrl();
//        dbProperties.put("create", "true");
//        
//        try {
//            connection = DriverManager.getConnection(dbUrl, dbProperties);
//            bCreated = createTables(connection);
//        } catch (SQLException ex) {
//        }
//        dbProperties.remove("create");
//        return bCreated;
//    }
//    
//    
//
//    
//    private String getHomeDir() {
//        return System.getProperty("user.home");
//    }
//    
//    public void disconnect() {
//        if(isConnected) {
//            String dbUrl = getDatabaseUrl();
//            dbProperties.put("shutdown", "true");
//            try {
//                DriverManager.getConnection(dbUrl, dbProperties);
//            } catch (SQLException ex) {
//            }
//            isConnected = false;
//        }
//    }
//    
//    public String getDatabaseLocation() {
//        String dbLocation = System.getProperty("derby.system.home") + "/" + dbName;
//        return dbLocation;
//    }
//    
//    public String getDatabaseUrl() {
//        String dbUrl = dbProperties.getProperty("derby.url") + dbName;
//        return dbUrl;
//    }
    
    
//    private String dbName;
//    private Connection dbConnection;
//    private Properties dbProperties;
//    private boolean isConnected;
//  
//   /* private String strCreateRegionTable = "CREATE TABLE regions (" 
//            + " rgId integer  NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), " 
//            + " parentId integer, " 
//            + "rgName varchar(80) )";
//    
//    */
//    
//    
//    
//    private PreparedStatement stmtGetListFolders;
//    private PreparedStatement stmtSaveFolder;
//
//    private PreparedStatement stmtUpdateFolder;
//    
//    private PreparedStatement stmtDeleteFolder;
//       
//            
//    
//    private static final String strGetListFolders = 
//            "SELECT ID, NAME FROM FOLDER " +
//            "WHERE PARENTID = ? " +
//            "ORDER BY NAME ASC";
//
//   private static final String strSaveFolder = 
//            "INSERT INTO FOLDER " +
//            "(NAME, PARENTID) " +
//            "VALUES (?, ?)";       
//   
//
//   
//   private static final String strUpdateFolder = 
//            "UPDATE FOLDER " +
//            "SET NAME = ? " +
//            "WHERE ID = ?";       
// 
//    
//    private static final String strDeletePrimer =
//            "DELETE FROM PRIMER " +
//            "WHERE ID = ?";
//
//    private static final String strDeleteFolder =
//            "DELETE FROM FOLDER " +
//            "WHERE ID = ?";
 
    

}




/*
 * AddressDao.java
 *
 * Copyright 2006 Sun Microsystems, Inc. ALL RIGHTS RESERVED Use of 
 * this software is authorized pursuant to the terms of the license 
 * found at http://developers.sun.com/berkeley_license.html .

 */


//import java.io.File;
//import java.io.IOException;
//import java.io.InputStream;
//import java.sql.DriverManager;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.sql.Connection;
//import java.sql.Statement;
//import java.util.ArrayList;
//import java.util.Vector;
//import java.util.Properties;
//
///**
// *
// * @author John O'Conner
// */
//public class PrimerDao {
//    
//    /** Creates a new instance of AddressDao */
//    public PrimerDao() {
//        this("PrimerDatabase");
//    }
//    
//    public PrimerDao(String primerName) {
//        this.dbName = primerName;
//        
//        setDBSystemDir();
//        dbProperties = loadDBProperties();
//        String driverName = dbProperties.getProperty("derby.driver"); 
//        loadDatabaseDriver(driverName);
//        if(!dbExists()) {
//            Boolean b = createDatabase();
//            if(b) {
//               System.out.println("Database created successfully");
//            }
//        }
//        
//    }
//    
//    private boolean dbExists() {
//        boolean bExists = false;
//        String dbLocation = getDatabaseLocation();
//        File dbFileDir = new File(dbLocation);
//        if (dbFileDir.exists()) {
//            bExists = true;
//        }
//        return bExists;
//    }
//    
//    private void setDBSystemDir() {
//        // decide on the db system directory
//        String userHomeDir = System.getProperty("user.home", ".");
//        String systemDir = ".primerdb";
//        System.setProperty("derby.system.home", systemDir);
//        
//        // create the db system directory
//        File fileSystemDir = new File(systemDir);
//        fileSystemDir.mkdir();
//    }
//    
//    private void loadDatabaseDriver(String driverName) {
//        // load Derby driver
//        try {
//            Class.forName(driverName);
//        } catch (ClassNotFoundException ex) {
//            ex.printStackTrace();
//        }
//    }
//    
//    private Properties loadDBProperties() {
//        InputStream dbPropInputStream = null;
//        dbPropInputStream = PrimerDao.class.getResourceAsStream("Configuration.properties");
//        dbProperties = new Properties();
//        try {
//            dbProperties.load(dbPropInputStream);
//        } catch (IOException ex) {
//            ex.printStackTrace();
//        }
//        return dbProperties;
//    }
//    
//    
//    private boolean createTables(Connection dbConnection) {
//        boolean bCreatedTables = false;
//        Statement statement = null;
//        try {
//            statement = dbConnection.createStatement();
//            statement.execute(strCreatePrimerTable);
//            statement.execute(strCreateProjectTable);
//            statement.execute(strInsertRootFolder);
//            bCreatedTables = true;
//        } catch (SQLException ex) {
//            ex.printStackTrace();
//        }
//        
//        return bCreatedTables;
//    }
//    private boolean createDatabase() {
//        boolean bCreated = false;
//        Connection dbConnection = null;
//        
//        String dbUrl = getDatabaseUrl();
//        dbProperties.put("create", "true");
//        
//        try {
//            dbConnection = DriverManager.getConnection(dbUrl, dbProperties);
//            bCreated = createTables(dbConnection);
//        } catch (SQLException ex) {
//        }
//        dbProperties.remove("create");
//        return bCreated;
//    }
//    
//    
//    public boolean connect() {
//        String dbUrl = getDatabaseUrl();
//        try {
//            dbConnection = DriverManager.getConnection(dbUrl, dbProperties);
//
//           
//            
//            stmtSavePrimer = dbConnection.prepareStatement(strSavePrimer, Statement.RETURN_GENERATED_KEYS);
//            stmtSaveFolder = dbConnection.prepareStatement(strSaveFolder, Statement.RETURN_GENERATED_KEYS);
//            
//            
//            stmtUpdatePrimer = dbConnection.prepareStatement(strUpdatePrimer);
//            stmtUpdateFolder = dbConnection.prepareStatement(strUpdateFolder);
//            
//            stmtGetPrimer = dbConnection.prepareStatement(strGetPrimer);
//            stmtGetListPrimers = dbConnection.prepareStatement(strGetListPrimers);
//            stmtGetListFolders = dbConnection.prepareStatement(strGetListFolders);
//
//                        
//            stmtDeletePrimer = dbConnection.prepareStatement(strDeletePrimer);
//            stmtDeleteFolder = dbConnection.prepareStatement(strDeleteFolder);            
//
//            
//            isConnected = dbConnection != null;
//        } catch (SQLException ex) {
//            ex.printStackTrace();
//            isConnected = false;
//        }
//        return isConnected;
//    }
//    
//    private String getHomeDir() {
//        return System.getProperty("user.home");
//    }
//    
//    public void disconnect() {
//        if(isConnected) {
//            String dbUrl = getDatabaseUrl();
//            dbProperties.put("shutdown", "true");
//            try {
//                DriverManager.getConnection(dbUrl, dbProperties);
//            } catch (SQLException ex) {
//            }
//            isConnected = false;
//        }
//    }
//    
//    public String getDatabaseLocation() {
//        String dbLocation = System.getProperty("derby.system.home") + "/" + dbName;
//        return dbLocation;
//    }
//    
//    public String getDatabaseUrl() {
//        String dbUrl = dbProperties.getProperty("derby.url") + dbName;
//        return dbUrl;
//    }
//    
//    
//    
//    public int newFolder(Folder f) {
//        int id = -1;
//        try {
//            stmtSaveFolder.clearParameters();
//            
//            stmtSaveFolder.setString(1, f.getName());
//            stmtSaveFolder.setInt(2, f.getParentId());
//            int rowCount = stmtSaveFolder.executeUpdate();
//            ResultSet results = stmtSaveFolder.getGeneratedKeys();
//            if (results.next()) {
//                id = results.getInt(1);
//            }
//            
//        } catch(SQLException sqle) {
//            sqle.printStackTrace();
//        }
//        return id;
//    }
//    
//      
//    public int newPrimer(Primer record) {
//        int id = -1;
//        try {
//            stmtSavePrimer.clearParameters();
//            
//            stmtSavePrimer.setString(1, record.getName());
//            stmtSavePrimer.setString(2, record.getAnnTemp());
//            stmtSavePrimer.setString(3, record.getFreezer());
//            stmtSavePrimer.setString(4, record.getTarget());
//            stmtSavePrimer.setString(5, record.getConc());
//            stmtSavePrimer.setString(6, record.getDate());
//            stmtSavePrimer.setString(7, record.getLabel());
//            stmtSavePrimer.setString(8, record.getSequence());
//            stmtSavePrimer.setString(9, record.getNotes());
//            stmtSavePrimer.setString(10, record.getProject());
//            stmtSavePrimer.setInt(11, record.getFolderId());
//            int rowCount = stmtSavePrimer.executeUpdate();
//            ResultSet results = stmtSavePrimer.getGeneratedKeys();
//            if (results.next()) {
//                id = results.getInt(1);
//            }
//            
//        } catch(SQLException sqle) {
//            sqle.printStackTrace();
//        }
//        return id;
//    }
//    
//    
//    public boolean editFolderName(int id, String name) {
//       
//       System.out.println("editFolderName with id=" + Integer.toString(id));
//       
//        boolean bEdited = false;
//        try {
//            stmtUpdateFolder.clearParameters();
//            stmtUpdateFolder.setString(1, name); 
//            stmtUpdateFolder.setInt(2, id);
//            stmtUpdateFolder.executeUpdate();
//            bEdited = true;
//        } catch(SQLException sqle) {
//            sqle.printStackTrace();
//        }
//        return bEdited;
//        
//    }
//    
//    
//    public boolean editPrimer(Primer p) {
//       
//       System.out.println("editPrimer with id=" + Integer.toString(p.getId()));
//       
//        boolean bEdited = false;
//        try {
//            stmtUpdatePrimer.clearParameters();
//            
//            stmtUpdatePrimer.setString(1, p.getName());
//            stmtUpdatePrimer.setString(2, p.getAnnTemp());
//            stmtUpdatePrimer.setString(3, p.getFreezer());
//            stmtUpdatePrimer.setString(4, p.getTarget());
//            stmtUpdatePrimer.setString(5, p.getConc());
//            stmtUpdatePrimer.setString(6, p.getDate());
//            stmtUpdatePrimer.setString(7, p.getLabel());
//            stmtUpdatePrimer.setString(8, p.getSequence());
//            stmtUpdatePrimer.setString(9, p.getNotes());
//            stmtUpdatePrimer.setString(10, p.getProject());
//            stmtUpdatePrimer.setInt(11, p.getFolderId());            
//            stmtUpdatePrimer.setInt(12, p.getId());
//            stmtUpdatePrimer.executeUpdate();
//            bEdited = true;
//        } catch(SQLException sqle) {
//            sqle.printStackTrace();
//        }
//        return bEdited;
//        
//    }
//    
//    
//    
//    
//    public boolean deletePrimer(int id) {
//       
//       System.out.println("Deleting primer " + id);
//       
//        boolean bDeleted = false;
//        try {
//            //stmtDeletePrimer = dbConnection.prepareStatement(strDeletePrimer);
//            stmtDeletePrimer.clearParameters();
//            stmtDeletePrimer.setInt(1, id);
//            stmtDeletePrimer.executeUpdate();
//            bDeleted = true;
//        } catch (SQLException sqle) {
//           System.out.println("Exception:");
//            sqle.printStackTrace();
//        }
//        
//        return bDeleted;
//    }
//    
//
//    public boolean deleteFolder(int id) {
//       
//       System.out.println("Deleting folder " + id);
//       
//        boolean bDeleted = false;
//        try {
//            stmtDeleteFolder.clearParameters();
//            stmtDeleteFolder.setInt(1, id);
//            stmtDeleteFolder.executeUpdate();
//            bDeleted = true;
//        } catch (SQLException sqle) {
//            sqle.printStackTrace();
//        }
//        
//        return bDeleted;
//    }
//    
//    
//    
//    /*
//    public boolean deleteRecord(Address record) {
//        int id = record.getId();
//        return deleteRecord(id);
//    }
//    
//    */
//    
//    public Vector<Vector<String>> getListPrimers(int folderId) {
//       
//       System.out.println("getListPrimers function");
//       
//       Vector<Vector<String>> listEntries = new Vector<Vector<String>>();
//       //Statement queryStatement = null;
//       ResultSet results = null;
//        
//       try {
//           //queryStatement = dbConnection.createStatement();
//           
//           stmtGetListPrimers.clearParameters();
//           stmtGetListPrimers.setInt(1, folderId);
//          
//           
//           results = stmtGetListPrimers.executeQuery();
//           while(results.next()) {
//               int id = results.getInt(1);
//               String name = results.getString(2);
//               String annTemp = results.getString(3);
//               String target = results.getString(4);
//               String conc = results.getString(5);
//               String date = results.getString(6);
//               Vector<String> p = new Vector<String>();
//               p.add(Integer.toString(id));
//               p.add(name);
//               p.add(annTemp);
//               p.add(target);
//               p.add(conc);
//               p.add(date);
//
//               listEntries.add(p);
//           }
//           
//       } catch (SQLException sqle) {
//            sqle.printStackTrace();
//            
//        }
//        System.out.println(listEntries.toString());
//        return listEntries;
//    }
//
//    
//    public Vector<Folder> getListFolders(int parentFolderId) {
//       
//       System.out.println("getListFolder function");
//       
//       Vector<Folder> listEntries = new Vector<Folder>();
//       //Statement queryStatement = null;
//       ResultSet results = null;
//        
//       try {
//           //queryStatement = dbConnection.createStatement();
//           
//           stmtGetListFolders.clearParameters();
//           stmtGetListFolders.setInt(1, parentFolderId);
//          
//           
//           results = stmtGetListFolders.executeQuery();
//           while(results.next()) {
//               int id = results.getInt(1);
//               String name = results.getString(2);
// 
//               Folder f = new Folder(id, name, parentFolderId);
//
//               listEntries.add(f);
//           }
//           
//       } catch (SQLException sqle) {
//            sqle.printStackTrace();
//            
//        }
//        System.out.println(listEntries.toString());
//        return listEntries;
//    }
//    
//    
//
//    public Primer getPrimer(int index) {
//        System.out.println("Getting primer " + index);
//        Primer primer = null;
//        try {
//            stmtGetPrimer.clearParameters();
//            stmtGetPrimer.setInt(1, index);
//            ResultSet result = stmtGetPrimer.executeQuery();
//            if (result.next()) {
//                String name = result.getString("NAME");
//                String annTemp = result.getString("ANNTEMP");
//                String freezer = result.getString("FREEZER");
//                String target = result.getString("TARGET");
//                String conc = result.getString("CONC");
//                String date = result.getString("DATE");
//                String label = result.getString("LABEL");                
//                String sequence = result.getString("SEQUENCE");
//                String notes = result.getString("NOTES");
//                String project = result.getString("PROJECT");
//                int folderId = result.getInt("FOLDERID");
//                int id = result.getInt("ID");
//                primer = new Primer(name, annTemp, freezer, target, conc, date, label, sequence, notes, project, folderId, id);
//            }
//        } catch(SQLException sqle) {
//            sqle.printStackTrace();
//        }
//        
//        return primer;
//    }
//    
//    public static void main(String[] args) {
//        PrimerDao db = new PrimerDao();
//        System.out.println(db.getDatabaseLocation());
//        System.out.println(db.getDatabaseUrl());
//        boolean b = db.connect();
//        
//        System.out.println(b);
//        
//        Project proj = new Project("testproj", 2);
//       
//        /*
//        Primer p = new Primer("test", "acddgd", "12C", proj);
//        
//        int id = db.newPrimer(p);
//        
//        p.setId(id);
//        
//        Primer newp = db.getPrimer(2);
//        
//        System.out.println(newp.toString());
//        */
//        
//        db.disconnect();
//    }
//    
//    
//    private int rootFolderId = 1;
//    
//    private Connection dbConnection;
//    private Properties dbProperties;
//    private boolean isConnected;
//    private String dbName;
//    private PreparedStatement stmtSavePrimer;    
//    private PreparedStatement stmtGetPrimer;
//    private PreparedStatement stmtGetListPrimers;
//    private PreparedStatement stmtUpdatePrimer;
//    private PreparedStatement stmtDeletePrimer;
//    
//    
//    
//    private static final String strCreatePrimerTable =
//            "create table PRIMER (" +
//            "    ID          INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)," +
//            "    NAME        VARCHAR(100), " +
//            "    FREEZER     VARCHAR(100), " +
//            "    LABEL       VARCHAR(100), " +
//            "    DATE        VARCHAR(100), " +
//            "    TARGET      VARCHAR(100), " +
//            "    SEQUENCE    VARCHAR(10000), " +
//            "    NOTES       VARCHAR(10000), " +
//            "    ANNTEMP     VARCHAR(100), " +
//            "    CONC        VARCHAR(100), " +
//            "    PROJECT     VARCHAR(1000), " +
//            "    FOLDERID   INTEGER " + 
//            ")";
//
//    private static final String strCreateProjectTable =
//            "create table FOLDER (" +
//            "    ID     INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)," +
//            "    NAME   VARCHAR(100), " +
//            "    PARENTID   INTEGER " +
//            ")";
//    
//    private static final String strInsertRootFolder =
//            "INSERT INTO FOLDER (NAME, PARENTID) VALUES ('ROOT', 1)";
//    
//    
//
//    
//    private static final String strGetPrimer =
//            "SELECT * FROM PRIMER " +
//            "WHERE ID = ?";
//    
//    private static final String strSavePrimer =
//            "INSERT INTO PRIMER " +
//            "   (NAME, ANNTEMP, FREEZER, TARGET, CONC, DATE, LABEL, SEQUENCE, NOTES, PROJECT, FOLDERID) " +
//            " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
//    
//    
//    private static final String strGetListPrimers =
//            "SELECT ID, NAME, ANNTEMP, TARGET, CONC, DATE FROM PRIMER "  +
//            "WHERE FOLDERID = ? " +
//            "ORDER BY NAME ASC";
//    
//    
//    private static final String strUpdatePrimer =
//            "UPDATE PRIMER " +
//            "SET NAME = ?, " +
//            "    ANNTEMP = ?, " +
//            "    FREEZER = ?, " +
//            "    TARGET = ?, " +
//            "    CONC = ?, " +
//            "    DATE = ?, " +
//            "    LABEL = ?, " +
//            "    SEQUENCE = ?, " +
//            "    NOTES = ?, " +
//            "    PROJECT = ?, " +
//            "    FOLDERID = ? " +
//            "WHERE ID = ?";    
//
//    private String strInsertRoot = "INSERT INTO regions (parentId,rgName) VALUES ('0','World')";
//    
//}
